﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Base;

namespace WeifenLuo.WinFormsUI.Security
{
    public partial class FormLogin : Form
    {
        public MainBaseForm mMainBaseForm;
        public Ctrl_Security mCtrlScur;

        public FormLogin()
        {
            InitializeComponent();
        }

        public string UserName
        { get { return txtUsername.Text.Trim(); } }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            mMainBaseForm = (MainBaseForm)this.Owner;
            txtUsername.Text = GetUsernameFromRegistry();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            // set new connection string with new DB Name
            ((MainBaseForm)this.Owner).stbDBName.Text = "DBName" + ": " + ((MainBaseForm)this.Owner).mCtrlDB.GetDBName();
            if (CheckLogin()) DialogResult = DialogResult.OK;
            SaveUsernameToRegistry(txtUsername.Text.Trim());
            Cursor = Cursors.Default;
            //MessageBox.Show(SystemInformation.UserName, SystemInformation.UserDomainName);
            //MessageBox.Show(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
        }


        public bool CheckLogin()
        {
            mCtrlScur = new Ctrl_Security();
            switch (mCtrlScur.CheckLogin(txtUsername.Text.Trim(), txtPassword.Text.Trim()))
            {
                case -1:
                    MessageBox.Show("Please enter the user name!", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    txtPassword.Focus();
                    return false;

                case -2:
                    MessageBox.Show("Please enter your password!", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    txtPassword.Focus();
                    return false;

                case -3:
                    MessageBox.Show("Invalid user name", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    txtUsername.Focus();
                    return false;

                case -4:
                    MessageBox.Show("User is locked.", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    txtUsername.Focus();
                    return false;

                case -5:
                    MessageBox.Show("Password is not correct.", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    txtPassword.SelectAll();
                    txtPassword.Focus();
                    return false;

                case -6:
                case -7:
                    MessageBox.Show("Logged in failed. Please check the Database connection.", "title", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return false;

                default:
                    return true;
            }
        }

        private string GetUsernameFromRegistry()
        {   //Registry is an abstract class, so we do nothing on it.
            RegistryKey HkCu = Registry.CurrentUser.OpenSubKey("Software");
            RegistryKey HkMine = HkCu.OpenSubKey(ConstantKeys.KeyInRegistry);

            string ls_userName = HkMine.GetValue("UserName", "").ToString();
            HkMine.Close();
            return ls_userName;
        }

        private void SaveUsernameToRegistry(string as_UserName)
        {   // First, we have to navigate to the HKEY_CURRENT_USER/Software/aKeyInSoftWare registry key
            RegistryKey HkCu = Registry.CurrentUser.OpenSubKey("Software", true);
            RegistryKey HkMine = HkCu.OpenSubKey(ConstantKeys.KeyInRegistry, true);
            HkMine.SetValue("UserName", as_UserName);
            HkMine.Close();
        }



    }
}
